<template>
  <div class="content_box">
    <div class="coupon-item">
        <!-- <div class="left">
          <p class="name">{{ msg.name }}</p>
          <p class="text"><span>使用时长：{{ msg.hours }}小时</span> <span>有效天数：{{ msg.validDay }}天</span></p>
        </div>
        <div class="right">
          <p class="price">￥<span>{{ msg.sellPrice }}</span></p>
        </div>
      </div> -->
      <div class="box" >
        <img class="bj" src="/static/images/coupon_bj.png" alt="">
        <p class="label" >限时折扣</p>
        <div class="nr">
          <div class="left">
            <p class="price"><span>￥</span>{{msg.sellPrice}}</p>
           
          </div>
          <p class="line"></p>
          <div class="right">
            <div class="name_box">
              <div class="name">
                <p class="title" >{{ msg.name }}* {{ msg.bindNum }}张</p>
                <p class="time">有效期天数：{{msg.validDay}}</p>
                <p class="text">剩余库存：{{msg.stock}}</p>
              </div>
              <!-- <p class="anniu"  @click="toHome">去购买</p> -->
            </div>
            <p class="jiesao">{{msg.remark}}</p>
          </div>
        </div>
      </div>
    <div class="star">
      <van-checkbox 
        size="152px"
        checked-color="#00C200" 
        icon-size="16px" 
        :value="checked" 
        :disabled="userStarNum < msg.starVale"
        @change="changeCheckbox">
        <div class="item">
          <p>账户攒星数：<span>{{ userStarNum }} </span>颗</p>
          <p>卡券需要 {{ msg.starVale }} 颗星可兑换</p>
        </div>
      </van-checkbox>
    </div>
    <div class="tips">
      <p class="name">温馨提示</p>
      <p class="text">1.当前优惠卡卷 “ <span style="font-size: 30rpx;font-weight: 500;">{{msg.name}}</span>” 购买后，账户内实得 <span style="color: #E93B3B;font-size: 36rpx;font-weight: 500;padding: 0 5px;">{{ msg.bindNum}}</span> 张；</p>
      <p class="text">2.卡卷仅限购买时使用；</p>
      <p class="text">3.卡卷不找零、不兑现、不与其他优惠同享</p>
    </div>
    <div class="totle-price" v-if="!checked">
      <div class="price">总计：<span style="font-size: 12px;">￥</span><span>{{ msg.sellPrice }}</span></div>
      <div class="anniu" @click="submit">立即支付</div>
    </div>
    <div class="totle-price" v-else>
      <div class="anniu" v-if="msg.stock" style="width: 100%;" @click="duihuan">立即兑换</div>
      <div class="anniu disable" v-else style="width: 100%;" >账户攒星余额不足</div>
    </div>
  </div>
</div>
</template>

<script>
import { Debounce } from "@/utils"
import {
  getUserStarRecord,
  getCardCouponDetails,
  addCardCouponRecord,
  userGetCardCoupon,
  getCardCouponStartPay
} from "@/api/index";
export default {
  data() {
    return {
      checked:false,
      msg: {},
      userStarNum:0
    };
  },

  methods: {

    // 确认兑换
    duihuan:Debounce(function () {
      this.$showLoading('兑换中...')
      this.loading = true
      userGetCardCoupon({cardCouponId:this.msg.id}).then(res=>{
        if(res.statusCode == '00000'){
          setTimeout(()=>{
            this.$toast('已兑换')
            wx.redirectTo({
              url: `/pages/home/cardList/main`,
            });
          },1000)
        }else{
          wx.hideLoading()
          this.$toast(res.message)
        }
      })
    },300),

    // 发起支付
    submit:Debounce(async function () {
      const that = this
      this.$showLoading('支付中...')
      let cardCouponRecordId = null
      await addCardCouponRecord({
        cardCouponId: this.msg.id,
      }).then(res=>{
        if(res.statusCode === '00000'){
          cardCouponRecordId = res.data
        } else {
          wx.hideLoading()
          that.$toast(res.message)
        }
      })
      if(cardCouponRecordId){
        await getCardCouponStartPay({cardCouponRecordId}).then(res=>{
          if(res.statusCode == '00000'){
            wx.requestPayment({
              timeStamp:res.data.timeStamp,
              nonceStr:res.data.nonceStr,
              package:res.data.prepayId,
              signType:res.data.signType,
              paySign:res.data.paySign,
              success:function(succ){
                //console.log(succ)
                if(succ.errMsg === "requestPayment:ok"){
                  setTimeout(()=>{
                    that.$toast('已支付~')
                    wx.redirectTo({
                      url: `/pages/daka/main`,
                    });
                  },1000)
                }
              },
              fail:function(err){
                wx.hideLoading()
                that.$toast('支付失败~')
              }
            })
          }else{
            wx.hideLoading()
            that.$toast('支付失败~')
          }
        })
      }
    },300),

    changeCheckbox(e){
      //console.log(e)
      const that=this
      if(this.checked){
        this.checked=!this.checked
        return
      } 
      this.checked=true
    },
    async getDetail(id) {
      const res = await getCardCouponDetails({
        id: id
      });
      //console.log(res)
      if (res.statusCode === '00000') {
        this.msg = res.data;
      }
    },

    // 获取攒星日期
    async getUserStar(){
      await getUserStarRecord().then(res=>{
        if(res.statusCode === '00000'){
          this.userStarNum = res.data.starValue
        }
      })
    },
  },
  onLoad(options){
    //console.log('111')
    this.getDetail(options.id)
    this.getUserStar()
  },
  onShow() {
    this.checked=false
  }
};
</script>

<style lang="scss" scoped>
.content_box{
  width: 100%;
  min-height: 100vh;
  background: #F5F5F5;
  .coupon-item {
    padding-top: 24rpx;
    .box{
      width: 92%;
      height: 208rpx;
      position: relative;
      margin:0 auto;
      margin-bottom:24rpx;
      .bj{
        width: 100%;
        height: 100%;
        position: absolute;
        top:0;
        left:0;
        z-index: 10;
      }
      .bj1{
        width: 100%;
        height: 100%;
        position: absolute;
        top:15rpx;
        left:0;
        z-index: 1;
      }
      .label{
        position: absolute;
        top:0;
        left: 6rpx ;
        padding:0 16rpx;
        height: 40rpx;
        background: linear-gradient(to right,#FF4357, #FE6E34);
        z-index: 20;
        font-size:22rpx;
        color:#fff;
        display: flex;
        align-items: center;
        justify-content: center;
        border-top-left-radius: 16rpx;
        border-bottom-right-radius: 16rpx;
        font-weight: 500;
      }
      .nr{
        position: relative;
        z-index: 30;
        display: flex;
        .left{
          width: 29%;
          height: 208rpx;
          display: flex;
          flex-direction:column;
          justify-content: center;
          align-items: center;
          .price{
            display: flex;
            align-items: baseline;
            font-size:70rpx;
            font-weight: bold;
            color:#FF4622;
            span{
              font-size:30rpx;
            }
          }
         
        }  
        .line{
          width: 1rpx;
          height: 160rpx;
          border-right:1px dashed #E4E4E4;
          margin-top:22rpx;
        }
        .right{
          flex: 1;
          padding-left:24rpx;
          .name_box{
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            margin-top:40rpx;
            margin-bottom:28rpx;
            .name{
              padding-top: 10px;
              padding-left: 20rpx;
              .title{
                color:#242526;
                font-size:32rpx;
                font-weight: 500;
                margin-bottom:12rpx;
                
              }
              .time{
                color:#666666;
                font-size:24rpx;
              }
              .text{
            font-size:24rpx;
            color:#FF4622;
            margin-top: 2px;
          }
            }
            img{
              width: 124rpx;
              height: 124rpx;
              position: absolute;
              right: 6rpx;
              top:8rpx;
            }
            .anniu{
              width: 114rpx;
              height: 50rpx;
              background: linear-gradient(to right,#FF4357, #FE6E34);
              font-size:24rpx;
              color:#fff;
              font-weight: bold;
              border-radius: 50rpx;
              display: flex;
              align-items: center;
              justify-content: center;
              margin-right:22rpx;
            }
          }
          .jiesao{
            color:#999999;
            font-size:22rpx;
          }
        }      
      }
      &.on{
        .label{
          color:#383838;
          background: #F6F6F8 ;
        }
        .price{
          color:#AEAFB0 !important;
        }
        .text{
          color:#B0B1B4 !important ;
        }
        .title{
          color:#8D8E90 !important;
        }
        .time{
          color:#AEAFB0 !important ;
        }
        .jiesao{
          color:#C8C8CA !important;
        }
      }
    }
  }
  .star {
    background: #fff;
    margin: 0rpx 30rpx;
    border-radius: 16rpx;
    padding: 30rpx 20rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    div {
      font-size: 26rpx;
      color: #333;
      span {
        color: #00C200;
        font-weight: 600;
        font-size: 28rpx;
      }
    }
    /deep/ ._van-checkbox {
      width: 100% !important;
    }
    /deep/ .van-checkbox {
      display: flex;
      justify-content: space-between;
    }
    /deep/ .van-checkbox__icon-wrap {
      order: 2;
    }
    /deep/ .van-checkbox__label {
      flex: 1;
    }
  }
  .tips {
    margin: 60rpx 32rpx 0rpx 32rpx;
    border-radius: 16rpx;
    .name {
      font-size: 30rpx;
      color: #333;
      font-weight: 600;
      margin-bottom: 20rpx;
    }
    .text {
      font-size: 28rpx;
      color: #666;
      line-height: 36rpx;
      margin-bottom: 20rpx;
    }
  }
  .totle-price {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 92%;
    height: 120rpx;
    background: #fff;
    box-shadow: 0 -2rpx 10rpx rgba(0,0,0,.1);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0rpx 30rpx;
    .price {
      font-size: 30rpx;
      color: #333;
      // font-weight: 600;
      span {
        font-size: 48rpx;
        color: #E93B3B;
        font-weight: 700;
      }
    }
    .anniu {
      width: 200rpx;
      height: 80rpx;
      line-height: 80rpx;
      text-align: center;
      color: #fff;
      background: #00C200;
      border-radius: 90rpx;
      font-size: 30rpx;
      font-weight: 500;
    }
    .disable{
      background: rgba(0,0,0,.3);
    }
  }
}
</style>